package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.mapper.BaseAttrInfoMapper;
import com.atguigu.gmall.product.model.BaseAttrInfo;
import com.atguigu.gmall.product.model.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.awt.geom.QuadCurve2D;
import java.util.List;
@Service
public class BaseAttrInfoServiceImpl extends ServiceImpl<BaseAttrInfoMapper, BaseAttrInfo> implements BaseAttrInfoService{
    @Autowired
    private BaseAttrValueService baseAttrValueService;
    /*
     * 根据id查找平台属性
     * */
    @Override
    public List<BaseAttrInfo> attrInfoList(Long category1Id, Long category2Id, Long category3Id) {
        BaseAttrInfoMapper baseMapper1 = this.baseMapper;

       List<BaseAttrInfo> baseAttrInfoList=    baseMapper1.attrInfoList(category1Id,category2Id,category3Id);
        return baseAttrInfoList;
    }

    @Override
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {
       //1.todo 判断新增属性是否存在
        //2. 看是否携带ID
        if (baseAttrInfo.getId()==null) {
            this.save(baseAttrInfo);
            List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
            if (!CollectionUtils.isEmpty(attrValueList)) {
                attrValueList.stream().forEach((baseAttrValue) -> {
                    baseAttrValue.setAttrId(baseAttrInfo.getId());
                });
                baseAttrValueService.saveBatch(attrValueList);
            }
        } else {
            this.updateById(baseAttrInfo);
            //先删除
            LambdaQueryWrapper<BaseAttrValue> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BaseAttrValue::getId, baseAttrInfo.getId());
            baseAttrValueService.remove(queryWrapper);
           //
            List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
            if (!CollectionUtils.isEmpty(attrValueList)) {
                attrValueList.stream().forEach((baseAttrValue) -> {
                    baseAttrValue.setAttrId(baseAttrInfo.getId());
                });
                baseAttrValueService.saveBatch(attrValueList);
            }

        }
    }
//根据attrId查找value值
    @Override
    public List<BaseAttrValue> getAttrValueList(Long attrId) {
        LambdaQueryWrapper<BaseAttrValue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseAttrValue::getAttrId,attrId);
        return baseAttrValueService.list(queryWrapper);
    }
}
